package gnss

import (
	"dgo/goutils/utils"
	"fmt"
	"math"
)

type Func_signconvert = func(sys byte, s byte) (d byte)

type SvConvertConf struct {
	SigConvertFunc Func_signconvert
	ObsVars        *RtcmObsVars
	GetWlFunc      Func_GetWl
}

var (
	GNSS_DEBUG_MODE int = 0
)

func GnssSvSignalFromRtcmRawSatSigObs(src *RtcmRawSatSigObs, dst *GnssSVSignal, getfreqfn Func_GetFreq, signconvert Func_signconvert) {
	dst.NaviSys = src.NaviSys
	if signconvert != nil {
		dst.SigId = signconvert(src.NaviSys, src.SigId)
	}
	if dst.SigId == 0 {
		dst.SigId = GnssSvSigIdFromRtcmSigID(src.NaviSys, src.SigId)
	}
	dst.SatId = uint16(src.SatId)
	dst.PsV = src.GetPsRng()
	dst.PhV = src.GetPhRng()
	dop := src.GetPhRR()
	if dop != 0 {
		var freq = float64(0)
		if getfreqfn != nil {
			freq = getfreqfn(dst.NaviSys, dst.SatId, dst.SigId)
		}
		freq = GetSigFreq(src.NaviSys, src.SigId, src.SatId)
		dop = dop * freq / CIGHT_SPEED
		// rtcm->obs.data[index].D[idx[k]]=(float)(-(rr[i]+rrf[j])*freq/CLIGHT);
	}
	dst.Doppler = dop
	dst.HalfAmb = src.HalfAmb
	dst.LkT = src.GetLockTime()
	dst.Cnr = src.Cnr
}

func GnssSvEpochFromRtcmMsm(epoch *GnssSVEpoch, msm RtcmRawMsm, copyRoughR bool, getfreqfn Func_GetFreq, signconvert Func_signconvert) {
	var sig GnssSVSignal
	msm.RangeObs(func(sigobs *RtcmRawSatSigObs) {
		sig.Reset()
		GnssSvSignalFromRtcmRawSatSigObs(sigobs, &sig, getfreqfn, signconvert)
		if sig.SigId == 0 {
			if GNSS_DEBUG_MODE == 1 {
				fmt.Printf("转换频点失败:%s->%s(%d)\r\n", GNSS_SYS_RINEX_CHAR[sigobs.NaviSys], SigiRinexCode(sigobs.NaviSys, sigobs.SigId), sigobs.SigId)
			}
			return
		}
		epoch.SetObsSignal(&sig)
		if copyRoughR {
			epoch.SetRoughR(sigobs.NaviSys, uint16(sigobs.SatId), sigobs.RoughRR, sigobs.RoughPRR)
		}
	})
}

func GnssSvSigIdFromRtcmSigID(navisys byte, sigid byte) (rval byte) {
	rval = 0
	switch navisys {
	case SYS_BD:
		switch sigid {
		case RTCM_SIG_BDS_B1_2I:
			rval = GNSS_SV_SIG_BDS_B1_2I

		case RTCM_SIG_BDS_B1_2Q:
			rval = GNSS_SV_SIG_BDS_B1_2Q

		case RTCM_SIG_BDS_B1_2X:
			rval = GNSS_SV_SIG_BDS_B1_2X

		case RTCM_SIG_BDS_B2_7I:
			rval = GNSS_SV_SIG_BDS_B2_7I

		case RTCM_SIG_BDS_B2_7Q:
			rval = GNSS_SV_SIG_BDS_B2_7Q

		case RTCM_SIG_BDS_B2_7X:
			rval = GNSS_SV_SIG_BDS_B2_7X

		case RTCM_SIG_BDS_B3_6I:
			rval = GNSS_SV_SIG_BDS_B3_6I

		case RTCM_SIG_BDS_B3_6Q:
			rval = GNSS_SV_SIG_BDS_B3_6Q

		case RTCM_SIG_BDS_B3_6X:
			rval = GNSS_SV_SIG_BDS_B3_6X

		case RTCM_SIG_BDS_B2a_5D:
			rval = GNSS_SV_SIG_BDS_B2a_5D

		case RTCM_SIG_BDS_B2a_5P:
			rval = GNSS_SV_SIG_BDS_B2a_5P

		case RTCM_SIG_BDS_B2a_5X:
			rval = GNSS_SV_SIG_BDS_B2a_5X

		case RTCM_SIG_BDS_B2b_7D:
			rval = GNSS_SV_SIG_BDS_B2b_7D

		case RTCM_SIG_BDS_B1C_1D:
			rval = GNSS_SV_SIG_BDS_B1C_1D

		case RTCM_SIG_BDS_B1C_1P:
			rval = GNSS_SV_SIG_BDS_B1C_1P

		case RTCM_SIG_BDS_B1C_1X:
			rval = GNSS_SV_SIG_BDS_B1C_1X
		}
	case SYS_GPS:
		switch sigid {
		case RTCM_SIG_GPS_L1_1C:
			rval = GNSS_SV_SIG_GPS_L1_1C

		case RTCM_SIG_GPS_L1_1P:
			rval = GNSS_SV_SIG_GPS_L1_1P

		case RTCM_SIG_GPS_L1_1W:
			rval = GNSS_SV_SIG_GPS_L1_1W

		case RTCM_SIG_GPS_L2_2C:
			rval = GNSS_SV_SIG_GPS_L2_2C

		case RTCM_SIG_GPS_L2_2P:
			rval = GNSS_SV_SIG_GPS_L2_2P

		case RTCM_SIG_GPS_L2_2W:
			rval = GNSS_SV_SIG_GPS_L2_2W

		case RTCM_SIG_GPS_L2_2S:
			rval = GNSS_SV_SIG_GPS_L2_2S

		case RTCM_SIG_GPS_L2_2L:
			rval = GNSS_SV_SIG_GPS_L2_2L

		case RTCM_SIG_GPS_L2_2X:
			rval = GNSS_SV_SIG_GPS_L2_2X

		case RTCM_SIG_GPS_L5_5I:
			rval = GNSS_SV_SIG_GPS_L5_5I

		case RTCM_SIG_GPS_L5_5Q:
			rval = GNSS_SV_SIG_GPS_L5_5Q

		case RTCM_SIG_GPS_L5_5X:
			rval = GNSS_SV_SIG_GPS_L5_5X

		case RTCM_SIG_GPS_L1_1S:
			rval = GNSS_SV_SIG_GPS_L1_1S

		case RTCM_SIG_GPS_L1_1L:
			rval = GNSS_SV_SIG_GPS_L1_1L

		case RTCM_SIG_GPS_L1_1X:
			rval = GNSS_SV_SIG_GPS_L1_1X

		}
	case SYS_GLO:
		switch sigid {
		case RTCM_SIG_GLO_G1_1C:
			rval = GNSS_SV_SIG_GLO_G1_1C

		case RTCM_SIG_GLO_G1_1P:
			rval = GNSS_SV_SIG_GLO_G1_1P

		case RTCM_SIG_GLO_G2_2C:
			rval = GNSS_SV_SIG_GLO_G2_2C

		case RTCM_SIG_GLO_G2_2P:
			rval = GNSS_SV_SIG_GLO_G2_2P

		}
	case SYS_GAL:
		switch sigid {
		case RTCM_SIG_GAL_E1_1C:
			rval = GNSS_SV_SIG_GAL_E1_1C

		case RTCM_SIG_GAL_E1_1A:
			rval = GNSS_SV_SIG_GAL_E1_1A

		case RTCM_SIG_GAL_E1_1B:
			rval = GNSS_SV_SIG_GAL_E1_1B

		case RTCM_SIG_GAL_E1_1X:
			rval = GNSS_SV_SIG_GAL_E1_1X

		case RTCM_SIG_GAL_E1_1Z:
			rval = GNSS_SV_SIG_GAL_E1_1Z

		case RTCM_SIG_GAL_E6_6C:
			rval = GNSS_SV_SIG_GAL_E6_6C

		case RTCM_SIG_GAL_E6_6A:
			rval = GNSS_SV_SIG_GAL_E6_6A

		case RTCM_SIG_GAL_E6_6B:
			rval = GNSS_SV_SIG_GAL_E6_6B

		case RTCM_SIG_GAL_E6_6X:
			rval = GNSS_SV_SIG_GAL_E6_6X

		case RTCM_SIG_GAL_E6_6Z:
			rval = GNSS_SV_SIG_GAL_E6_6Z

		case RTCM_SIG_GAL_E5b_7I:
			rval = GNSS_SV_SIG_GAL_E5b_7I

		case RTCM_SIG_GAL_E5b_7Q:
			rval = GNSS_SV_SIG_GAL_E5b_7Q

		case RTCM_SIG_GAL_E5b_7X:
			rval = GNSS_SV_SIG_GAL_E5b_7X

		case RTCM_SIG_GAL_E5ab_8I:
			rval = GNSS_SV_SIG_GAL_E5ab_8I

		case RTCM_SIG_GAL_E5ab_8Q:
			rval = GNSS_SV_SIG_GAL_E5ab_8Q

		case RTCM_SIG_GAL_E5ab_8X:
			rval = GNSS_SV_SIG_GAL_E5ab_8X

		case RTCM_SIG_GAL_E5a_5I:
			rval = GNSS_SV_SIG_GAL_E5a_5I

		case RTCM_SIG_GAL_E5a_5Q:
			rval = GNSS_SV_SIG_GAL_E5a_5Q

		case RTCM_SIG_GAL_E5a_5X:
			rval = GNSS_SV_SIG_GAL_E5a_5X

		}
	case SYS_SBS:
		switch sigid {
		case RTCM_SIG_SBAS_L1_1C:
			rval = GNSS_SV_SIG_SBAS_L1_1C

		case RTCM_SIG_SBAS_L5_5I:
			rval = GNSS_SV_SIG_SBAS_L5_5I

		case RTCM_SIG_SBAS_L5_5Q:
			rval = GNSS_SV_SIG_SBAS_L5_5Q

		case RTCM_SIG_SBAS_L5_5X:
			rval = GNSS_SV_SIG_SBAS_L5_5X

		}
	case SYS_QZS:
		switch sigid {
		case RTCM_SIG_QZS_L1_1C:
			rval = GNSS_SV_SIG_QZS_L1_1C

		case RTCM_SIG_QZS_L6_6S:
			rval = GNSS_SV_SIG_QZS_L6_6S

		case RTCM_SIG_QZS_L6_6L:
			rval = GNSS_SV_SIG_QZS_L6_6L

		case RTCM_SIG_QZS_L6_6X:
			rval = GNSS_SV_SIG_QZS_L6_6X

		case RTCM_SIG_QZS_L2_2S:
			rval = GNSS_SV_SIG_QZS_L2_2S

		case RTCM_SIG_QZS_L2_2L:
			rval = GNSS_SV_SIG_QZS_L2_2L

		case RTCM_SIG_QZS_L2_2X:
			rval = GNSS_SV_SIG_QZS_L2_2X

		case RTCM_SIG_QZS_L5_5I:
			rval = GNSS_SV_SIG_QZS_L5_5I

		case RTCM_SIG_QZS_L5_5Q:
			rval = GNSS_SV_SIG_QZS_L5_5Q

		case RTCM_SIG_QZS_L5_5X:
			rval = GNSS_SV_SIG_QZS_L5_5X

		case RTCM_SIG_QZS_L1_1S:
			rval = GNSS_SV_SIG_QZS_L1_1S

		case RTCM_SIG_QZS_L1_1L:
			rval = GNSS_SV_SIG_QZS_L1_1L

		case RTCM_SIG_QZS_L1_1X:
			rval = GNSS_SV_SIG_QZS_L1_1X
		}
	}
	return
}

/*
**

	转到RtcmSigID
*/
func GnssSvSigId2RtcmSigID(navisys byte, sigid byte) (rval byte) {
	rval = 0
	switch navisys {
	case SYS_BD:
		switch sigid {
		case GNSS_SV_SIG_BDS_B1_2I:
			rval = RTCM_SIG_BDS_B1_2I

		case GNSS_SV_SIG_BDS_B1_2Q:
			rval = RTCM_SIG_BDS_B1_2Q

		case GNSS_SV_SIG_BDS_B1_2X:
			rval = RTCM_SIG_BDS_B1_2X

		case GNSS_SV_SIG_BDS_B2_7I:
			rval = RTCM_SIG_BDS_B2_7I

		case GNSS_SV_SIG_BDS_B2_7Q:
			rval = RTCM_SIG_BDS_B2_7Q

		case GNSS_SV_SIG_BDS_B2_7X:
			rval = RTCM_SIG_BDS_B2_7X

		case GNSS_SV_SIG_BDS_B3_6I:
			rval = RTCM_SIG_BDS_B3_6I

		case GNSS_SV_SIG_BDS_B3_6Q:
			rval = RTCM_SIG_BDS_B3_6Q

		case GNSS_SV_SIG_BDS_B3_6X:
			rval = RTCM_SIG_BDS_B3_6X

		case GNSS_SV_SIG_BDS_B2a_5D:
			rval = RTCM_SIG_BDS_B2a_5D

		case GNSS_SV_SIG_BDS_B2a_5P:
			rval = RTCM_SIG_BDS_B2a_5P

		case GNSS_SV_SIG_BDS_B2a_5X:
			rval = RTCM_SIG_BDS_B2a_5X

		case GNSS_SV_SIG_BDS_B2b_7D:
			rval = RTCM_SIG_BDS_B2b_7D

		case GNSS_SV_SIG_BDS_B1C_1D:
			rval = RTCM_SIG_BDS_B1C_1D

		case GNSS_SV_SIG_BDS_B1C_1P:
			rval = RTCM_SIG_BDS_B1C_1P

		case GNSS_SV_SIG_BDS_B1C_1X:
			rval = RTCM_SIG_BDS_B1C_1X
		}
	case SYS_GPS:
		switch sigid {
		case GNSS_SV_SIG_GPS_L1_1C:
			rval = RTCM_SIG_GPS_L1_1C

		case GNSS_SV_SIG_GPS_L1_1P:
			rval = RTCM_SIG_GPS_L1_1P

		case GNSS_SV_SIG_GPS_L1_1W:
			rval = RTCM_SIG_GPS_L1_1W

		case GNSS_SV_SIG_GPS_L2_2C:
			rval = RTCM_SIG_GPS_L2_2C

		case GNSS_SV_SIG_GPS_L2_2P:
			rval = RTCM_SIG_GPS_L2_2P

		case GNSS_SV_SIG_GPS_L2_2W:
			rval = RTCM_SIG_GPS_L2_2W

		case GNSS_SV_SIG_GPS_L2_2S:
			rval = RTCM_SIG_GPS_L2_2S

		case GNSS_SV_SIG_GPS_L2_2L:
			rval = RTCM_SIG_GPS_L2_2L

		case GNSS_SV_SIG_GPS_L2_2X:
			rval = RTCM_SIG_GPS_L2_2X

		case GNSS_SV_SIG_GPS_L5_5I:
			rval = RTCM_SIG_GPS_L5_5I

		case GNSS_SV_SIG_GPS_L5_5Q:
			rval = RTCM_SIG_GPS_L5_5Q

		case GNSS_SV_SIG_GPS_L5_5X:
			rval = RTCM_SIG_GPS_L5_5X

		case GNSS_SV_SIG_GPS_L1_1S:
			rval = RTCM_SIG_GPS_L1_1S

		case GNSS_SV_SIG_GPS_L1_1L:
			rval = RTCM_SIG_GPS_L1_1L

		case GNSS_SV_SIG_GPS_L1_1X:
			rval = RTCM_SIG_GPS_L1_1X

		}
	case SYS_GLO:
		switch sigid {
		case GNSS_SV_SIG_GLO_G1_1C:
			rval = RTCM_SIG_GLO_G1_1C

		case GNSS_SV_SIG_GLO_G1_1P:
			rval = RTCM_SIG_GLO_G1_1P

		case GNSS_SV_SIG_GLO_G2_2C:
			rval = RTCM_SIG_GLO_G2_2C

		case GNSS_SV_SIG_GLO_G2_2P:
			rval = RTCM_SIG_GLO_G2_2P

		}
	case SYS_GAL:
		switch sigid {
		case GNSS_SV_SIG_GAL_E1_1C:
			rval = RTCM_SIG_GAL_E1_1C

		case GNSS_SV_SIG_GAL_E1_1A:
			rval = RTCM_SIG_GAL_E1_1A

		case GNSS_SV_SIG_GAL_E1_1B:
			rval = RTCM_SIG_GAL_E1_1B

		case GNSS_SV_SIG_GAL_E1_1X:
			rval = RTCM_SIG_GAL_E1_1X

		case GNSS_SV_SIG_GAL_E1_1Z:
			rval = RTCM_SIG_GAL_E1_1Z

		case GNSS_SV_SIG_GAL_E6_6C:
			rval = RTCM_SIG_GAL_E6_6C

		case GNSS_SV_SIG_GAL_E6_6A:
			rval = RTCM_SIG_GAL_E6_6A

		case GNSS_SV_SIG_GAL_E6_6B:
			rval = RTCM_SIG_GAL_E6_6B

		case GNSS_SV_SIG_GAL_E6_6X:
			rval = RTCM_SIG_GAL_E6_6X

		case GNSS_SV_SIG_GAL_E6_6Z:
			rval = RTCM_SIG_GAL_E6_6Z

		case GNSS_SV_SIG_GAL_E5b_7I:
			rval = RTCM_SIG_GAL_E5b_7I

		case GNSS_SV_SIG_GAL_E5b_7Q:
			rval = RTCM_SIG_GAL_E5b_7Q

		case GNSS_SV_SIG_GAL_E5b_7X:
			rval = RTCM_SIG_GAL_E5b_7X

		case GNSS_SV_SIG_GAL_E5ab_8I:
			rval = RTCM_SIG_GAL_E5ab_8I

		case GNSS_SV_SIG_GAL_E5ab_8Q:
			rval = RTCM_SIG_GAL_E5ab_8Q

		case GNSS_SV_SIG_GAL_E5ab_8X:
			rval = RTCM_SIG_GAL_E5ab_8X

		case GNSS_SV_SIG_GAL_E5a_5I:
			rval = RTCM_SIG_GAL_E5a_5I

		case GNSS_SV_SIG_GAL_E5a_5Q:
			rval = RTCM_SIG_GAL_E5a_5Q

		case GNSS_SV_SIG_GAL_E5a_5X:
			rval = RTCM_SIG_GAL_E5a_5X

		}
	case SYS_SBS:
		switch sigid {
		case GNSS_SV_SIG_SBAS_L1_1C:
			rval = RTCM_SIG_SBAS_L1_1C

		case GNSS_SV_SIG_SBAS_L5_5I:
			rval = RTCM_SIG_SBAS_L5_5I

		case GNSS_SV_SIG_SBAS_L5_5Q:
			rval = RTCM_SIG_SBAS_L5_5Q

		case GNSS_SV_SIG_SBAS_L5_5X:
			rval = RTCM_SIG_SBAS_L5_5X

		}
	case SYS_QZS:
		switch sigid {
		case GNSS_SV_SIG_QZS_L1_1C:
			rval = RTCM_SIG_QZS_L1_1C

		case GNSS_SV_SIG_QZS_L6_6S:
			rval = RTCM_SIG_QZS_L6_6S

		case GNSS_SV_SIG_QZS_L6_6L:
			rval = RTCM_SIG_QZS_L6_6L

		case GNSS_SV_SIG_QZS_L6_6X:
			rval = RTCM_SIG_QZS_L6_6X

		case GNSS_SV_SIG_QZS_L2_2S:
			rval = RTCM_SIG_QZS_L2_2S

		case GNSS_SV_SIG_QZS_L2_2L:
			rval = RTCM_SIG_QZS_L2_2L

		case GNSS_SV_SIG_QZS_L2_2X:
			rval = RTCM_SIG_QZS_L2_2X

		case GNSS_SV_SIG_QZS_L5_5I:
			rval = RTCM_SIG_QZS_L5_5I

		case GNSS_SV_SIG_QZS_L5_5Q:
			rval = RTCM_SIG_QZS_L5_5Q

		case GNSS_SV_SIG_QZS_L5_5X:
			rval = RTCM_SIG_QZS_L5_5X

		case GNSS_SV_SIG_QZS_L1_1S:
			rval = RTCM_SIG_QZS_L1_1S

		case GNSS_SV_SIG_QZS_L1_1L:
			rval = RTCM_SIG_QZS_L1_1L

		case GNSS_SV_SIG_QZS_L1_1X:
			rval = RTCM_SIG_QZS_L1_1X

		}
	}
	return
}

/*
将一颗卫星的Obs信息写入到RtcmRawMsmObs中
*/
func GnssSVObs2RtcmRawObsSatEx(src *GnssSVObs, dst *RtcmRawObsSat, convertconf *SvConvertConf) error {

	// 如果 RoughPRR, 先不写入
	if utils.GetU8BitValue0(dst.BitFlag, 0) == 0 && src.RoughPRR != 0 {
		dst.RoughPRR = src.RoughPRR
		dst.BitFlag = utils.SetU8BitValue0(dst.BitFlag, 0, true)
	}

	if src.RoughR != 0 {
		dst.RoughRR = src.RoughR
	} else {
		dst.RoughRR = 0
		//lst := make(sort.Float64Slice, 0, len(yobs.PsRngV))
		for i := U8_0; i < byte(len(src.PsRngV)); i++ {
			v := src.PsRngV[i]
			if v != 0 {
				dst.RoughRR = CalcuRoughRR_V1(v)
				break
			}
		}
	}

	if dst.RoughRR == 0 { //数据异常
		return fmt.Errorf("计算粗略范围值异常")
	}

	dstNaviSys := src.NaviSys

	//if dstNaviSys == SYS_GPS &&
	//	src.Satid == 17 {
	//	src.Satid = 17
	//}

	signconvert := convertconf.SigConvertFunc

	var obsVars *RtcmObsVars = convertconf.ObsVars

	for i := U8_0; i < byte(len(src.PsRngV)); i++ {
		if src.IsSignalValidIdx(i) {
			//v0 := src.PsRngV[i] - dst.RoughRR
			//if math.Abs(v0) > 293 { // 忽略该频点
			//	continue
			//}
			if dst.Satid == 0 {
				dst.Satid = byte(src.Satid)
			}

			// 信号编号
			srcSigId := i + 1

			dstSigId := U8_0

			if signconvert != nil {
				dstSigId = signconvert(src.NaviSys, srcSigId)
			}
			if dstSigId == 0 {
				dstSigId = GnssSvSigId2RtcmSigID(src.NaviSys, srcSigId)
			}

			if dstSigId == 0 {
				continue
			}

			if dstSigId >= RTCM_SIG_MIN_ID && dstSigId <= RTCM_SIG_MAX_ID {
				dst.SetPsPng(dstSigId, src.PsRngV[i])
				var wl float64
				if convertconf.GetWlFunc != nil {
					wl = convertconf.GetWlFunc(dstNaviSys, uint16(dst.Satid), dstSigId)
				}
				if wl == 0 {
					wl = SigWaveLen(dstNaviSys, dstSigId, dst.Satid)
				}

				dst.SetPhPngEx(dstNaviSys, dstSigId, src.PhRngV[i], &src.LLI[i], obsVars, wl)
				prr := src.Doppler[i]
				if prr != 0 {
					// wl = clight/freq
					// ppr * clight / freq = ppr * wl
					prr = prr * wl
				}
				// 第一个设置RoughPRR, 如果为0 prr存不下
				if (utils.GetU8BitValue0(dst.BitFlag, 0) == 0) && prr != 0 && wl > 0 {
					//v := math.Round(src.Doppler[i] / wl)
					// rrate[k]=ROUND(-data->D[j]*CLIGHT/freq)*1.0;
					v := math.Round(prr)
					dst.RoughPRR = int16(v)
					dst.BitFlag = utils.SetU8BitValue0(dst.BitFlag, 0, true)
				}
				// doppler
				dst.SetPhRR(dstNaviSys, dstSigId, prr)
				dst.SetCnr(dstSigId, src.Cnr[i])
				if dst.MsmType == 7 {
					dst.SetSigLockTV(dstSigId, uint16(LockTime2IndicatorMsm7(src.LkT[i])))
				} else {
					dst.SetSigLockTV(dstSigId, uint16(LockTime2Indicator(src.LkT[i])))
				}

				dst.SetSigLLI(dstSigId, src.LLI[i])

			}

		}
	}
	return nil
}

/*
将一颗卫星的Obs信息写入到RtcmRawMsmObs中
*/
func GnssSVObs2RtcmRawObsSat(src *GnssSVObs, dst *RtcmRawObsSat, vars *RtcmObsVars, signconvert Func_signconvert) error {
	convertconf := &SvConvertConf{
		ObsVars:        vars,
		SigConvertFunc: signconvert,
	}

	return GnssSVObs2RtcmRawObsSatEx(src, dst, convertconf)
}

func GnssSvEpoch2RtcmEpoch(src *GnssSVEpoch, dst *RtcmRawObsEpoch, vars *RtcmObsVars, signconvert Func_signconvert, onErr func(err error)) {

	dst.TOW = src.TOW
	var dstlst *RtcmRawMsmObs
	for i := U8_0; i < obsmap_len; i++ { // navisys
		dstlst = nil
		srclst := src.obsMap[i]
		srclst.Range(func(srcObs *GnssSVObs) bool {
			if dstlst == nil {
				dstlst = dst.CheckGetObs(srcObs.NaviSys)
			}
			dstlst.Header.NaviSys = srcObs.NaviSys
			dstlst.Header.TOW = src.TOW
			dstlst.Header.StaID = src.StaID

			if srcObs.Satid > 0 && srcObs.Satid < 64 {
				dstObs := dstlst.CheckGetSatObs(byte(srcObs.Satid))
				//dstObs.Satid = byte(srcObs.Satid)
				dstObs.MsmType = dst.MsmType
				err := GnssSVObs2RtcmRawObsSat(srcObs, dstObs, vars, signconvert)
				if err != nil && onErr != nil {
					if onErr != nil {
						onErr(err)
					} else {
						utils.Log.WarnTypef("rtcm", "[%s][tow:%d]数据异常:%s", srcObs.GetPrnCode(), src.TOW, err.Error())
					}
				}
			}
			return true
		})
	}
}

/**

 */

/*
*
千寻客服：2021-09-26 09:45:27
b1c 31 b2a 23
华测：
北斗B1C放在频点索引29上，B2a放在频点索引21上, 这个我们是从0开始计算, 我们是1D跟5C
*/
const (
	B2a_QX       = 23
	B2a_HC       = 22
	B2a_IGMAS_5X = 19

	B2b_IGMAS_7A = 20

	B1c_QX    = 31
	B1c_HC    = 30
	B1c_UB4B0 = 7 //
)

func OnSingalConvertFromRtcm(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case B2a_HC, B2a_QX, B2a_IGMAS_5X:
			d = GNSS_SV_SIG_BDS_B2a_5D
		case B1c_QX, B1c_HC, B1c_UB4B0:
			d = GNSS_SV_SIG_BDS_B1C_1D
		case 20:
			d = GNSS_SV_SIG_BDS_B2b_7D // igmas
		}
	}
	return
}

// "1D", "1P", "1X", // 5, 6, 7      (B1C) RINEX文档中, 但是没有具体编号 // UB4B0 非标
func OnSingalConvertFromRtcmUB4B0(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case B2a_HC, B2a_QX, B2a_IGMAS_5X:
			d = GNSS_SV_SIG_BDS_B2a_5D
		case 7:
			d = GNSS_SV_SIG_BDS_B1C_1X
		case 6:
			d = GNSS_SV_SIG_BDS_B1C_1P
		case 5:
			d = GNSS_SV_SIG_BDS_B1C_1D

		case 20:
			d = GNSS_SV_SIG_BDS_B2b_7D // igmas
		}
	}
	return
}

func OnSingalConvert2RtcmIgmas(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case GNSS_SV_SIG_BDS_B2a_5D:
			d = B2a_IGMAS_5X
		case GNSS_SV_SIG_BDS_B1C_1D:
			d = B1c_UB4B0
		}
	}
	return
}

func OnSingalConvert2RtcmUB4B0(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case GNSS_SV_SIG_BDS_B2a_5D:
			d = B2a_IGMAS_5X
		case GNSS_SV_SIG_BDS_B1C_1D:
			d = B1c_UB4B0
		}
	}
	return
}

func OnSingalConvert2RtcmKplAlg(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case GNSS_SV_SIG_BDS_B2a_5D:
			d = B2a_IGMAS_5X
		case GNSS_SV_SIG_BDS_B1C_1D, GNSS_SV_SIG_BDS_B1C_1P:
			d = B1c_UB4B0
		case GNSS_SV_SIG_BDS_B2b_7D:
			d = RTCM_BDS_SIGN_B2B_7D
		case GNSS_SV_SIG_BDS_B2ab_8P:
			d = 28 // 非标
		}
	}
	return
}

func OnSingalConvert2RtcmQx(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case GNSS_SV_SIG_BDS_B2a_5D, GNSS_SV_SIG_BDS_B2a_5P:
			d = B2a_QX
		case GNSS_SV_SIG_BDS_B1C_1D, GNSS_SV_SIG_BDS_B1C_1P, GNSS_SV_SIG_BDS_B1C_1X:
			d = B1c_QX
		case GNSS_SV_SIG_BDS_B2b_7D:
			d = RTCM_BDS_SIGN_B2B_7D // RTCM 10403.3 - Amendment 2
		}
	}
	return
}

// yd 和千寻一样
func OnSingalConvert2RtcmYd(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case GNSS_SV_SIG_BDS_B2a_5D, GNSS_SV_SIG_BDS_B2a_5P:
			d = B2a_QX
		case GNSS_SV_SIG_BDS_B1C_1D, GNSS_SV_SIG_BDS_B1C_1P, GNSS_SV_SIG_BDS_B1C_1X:
			d = B1c_QX
		case GNSS_SV_SIG_BDS_B2b_7D:
			d = RTCM_BDS_SIGN_B2B_7D // RTCM 10403.3 - Amendment 2
		}
	}
	return
}

func OnSingalConvert2RtcmHC(sys byte, s byte) (d byte) {
	if sys == SYS_BD {
		switch s {
		case GNSS_SV_SIG_BDS_B2a_5D:
			d = B2a_HC
		case GNSS_SV_SIG_BDS_B1C_1D, GNSS_SV_SIG_BDS_B1C_1P, GNSS_SV_SIG_BDS_B1C_1X:
			d = B1c_HC
		}
	}
	return
}
